package com.study.concurrent.lock;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest {

    private static Lock lock=new ReentrantLock();

    private static int count=0;

    private static CountDownLatch countDownLatch=new CountDownLatch(10000);

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                try {
                    lock.lock();
                    count++;
                }finally {
                    lock.unlock();
                }
//                count++;
                countDownLatch.countDown();
            },"Thread"+i+":").start();
        }
        countDownLatch.await();
        System.out.println("count = " + count);
    }

    private static void sleep(Integer millis){
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void sleepSecond(Integer seconds){
        try {
            Thread.sleep(1000*seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
